﻿using Microsoft.Xna.Framework;

namespace XNAssist
{
    /// <summary>
    /// A simple object that counts to a specified time, then triggers a variable
    /// </summary>
    /// <remarks> DON'T FORGET TO CALL THE UPDATE METHOD IN THE GAME LOOP! </remarks>
    public class Timer
    {
        /// <summary>
        /// The time that the timer has run.
        /// </summary>
        public float CurrentTime { get; private set; }

        /// <summary>
        /// The time that the timer will be triggered.
        /// </summary>
        public float TriggerTime { get; set; }

        /// <summary>
        /// Whether the timer is counting or not.
        /// </summary>
        public bool Running { get; private set; }

        /// <summary>
        /// Has the timer been running as long or longer than the trigger time?
        /// </summary>
        public bool TimerFinished
        {
            get { return CurrentTime >= TriggerTime; }
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="triggerTime"> The time that the timer will be triggered </param>
        public Timer(float triggerTime)
        {
            TriggerTime = triggerTime;
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="triggerTime"> The time that the timer will be triggered </param>
        /// <param name="running"> Whether the timer starts out running or not </param>
        public Timer(float triggerTime, bool running)
        {
            TriggerTime = triggerTime;
            Running = running;
        }

        /// <summary>
        /// Updates the timer
        /// </summary>
        /// <param name="gameTime"> The GameTime variable </param>
        public void Update(GameTime gameTime)
        {
            if (!Running) return;

            CurrentTime += (float) gameTime.ElapsedGameTime.TotalSeconds;
        }

        /// <summary>
        /// Starts the timer
        /// </summary>
        public void Start()
        {
            Running = true;
        }

        /// <summary>
        /// Restarts the timer (sets the time to zero)
        /// </summary>
        public void Restart()
        {
            CurrentTime = 0f;
            Running = true;
        }

        /// <summary>
        /// Pauses the timer
        /// </summary>
        public void Pause()
        {
            Running = false;
        }

        /// <summary>
        /// Stops the timer (stops it from running and resets the time to zero)
        /// </summary>
        public void Stop()
        {
            Running = false;
            CurrentTime = 0f;
        }

        /// <summary>
        /// Sets the time to zero
        /// </summary>
        public void Reset()
        {
            CurrentTime = 0f;
        }
    }
}
